#region Copyright Notices
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Library General Public License for more details.
// 
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace Yarr
{
    /// <summary>
    /// ThreadAdapter allows Yarr expressions to be evaluated on
    /// a new thread.
    /// </summary>
    internal class ThreadAdapter
    {
        private Environment environment;
        private Object expression;

        /// <summary>
        /// Creates a new ThreadAdapter that will evaluate expression
        /// within the given environment on a new Thread.
        /// TODO: Is environment thread safe ?
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="environment"></param>
        private ThreadAdapter(Object expression, Environment environment)
        {
            this.expression = expression;
            this.environment = environment;
        }

        /// <summary>
        /// Evalauates expression within the given environment on
        /// a new thread - Returns immediately, but the new thread 
        /// continues to execure expression asynchronously.
        /// </summary>
        /// <param name="expression">The expression to evaluate</param>
        /// <param name="environment">The local environment</param>
        /// <param name="apartmentState">The threading apartment state for the new thread</param>
        /// <returns>The new thread</returns>
        public static Thread Fork(Object expression, Environment environment, ApartmentState apartmentState)
        {
            ThreadAdapter threadAdapter = new ThreadAdapter(expression, environment);

            Thread thread = new Thread(new ThreadStart(threadAdapter.Invoke));
            thread.SetApartmentState(apartmentState);
            thread.Start();
            return thread;
        }

        /// <summary>
        /// Evaluate the expression
        /// </summary>
        private void Invoke()
        {
            try
            {
                Runtime.Eval(expression, environment);
            }
            catch (Exception e)
            {
                if (environment != null)
                    environment[Symbol.LAST_EXCPT] = e;
            }
        }
    }
}
